<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form inline size="small" :model="from" ref="from" :label-position="labelPosition" label-width="120px">
            <el-row>
              <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
                <el-form-item label="品种名称：">
                  <el-select v-model="from.commodityId" placeholder="请选择品种名称" class="selWidth" clearable>
                    <el-option v-for="item in options" :key="item.id" :label="item.commodityName" :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="合约代码：">
                  <el-input v-model="from.contractNo" placeholder="请输入合约代码" clearable class="selWidth" />
                </el-form-item>
                <el-form-item label="合约完整代码：">
                  <el-input v-model="from.contractNoFull" placeholder="请输入合约完整代码" clearable class="selWidth" />
                </el-form-item>
                <el-form-item label="状态：">
                  <el-select v-model="from.status" placeholder="请选择" class="selWidth" clearable>
                    <el-option :value="1" label="正常"></el-option>
                    <el-option :value="2" label="关闭"></el-option>
                    <el-option :value="3" label="锁定"></el-option>
                    <el-option :value="4" label="所有"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="24" :md="24" :lg="6" :xl="6" class="text-right from">
                <el-form-item>
                  <el-button class="mr10" size="small" icon="el-icon-search"  type="primary" @click="search()">搜索</el-button>
                  <el-button class="mr10" size="small" icon="el-icon-refresh" @click="reset()">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div class="btn_bt">
          <el-button class="mr10" size="small" icon="el-icon-circle-plus-outline"  type="primary" @click="add()">新增</el-button>
          <el-button class="mr10" size="small" icon="el-icon-delete"  type="danger" @click="allDel()">删除</el-button>
          <el-button class="mr10" size="small" icon="el-icon-top"  @click="exportTable()">导出表格</el-button>
        </div>
      </div>
      <el-table border ref="table" v-loading="loading" :data="tableData.data" style="width: 100%" size="mini"
        @selection-change="onSelectTab" highlight-current-row>
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="ID" min-width="80" />
        <el-table-column prop="commodityName" label="品种名称" min-width="80" />
        <el-table-column prop="exchangeName" label="交易所" min-width="100" />
        <el-table-column prop="contractNo" label="合约代码" min-width="80" />
        <el-table-column prop="contractNoFull" label="合约完整代码" min-width="100" />
        <el-table-column prop="contractName" label="合约名称" min-width="80" />
        <el-table-column prop="contractNameEn" label="合约英文名称" min-width="100" />


        <el-table-column prop="lockFlag" label="锁仓" min-width="80">
          <template slot-scope="scope">
            <div v-if="scope.row.lockFlag == 1">锁仓</div>
            <div v-if="scope.row.lockFlag == 2">不可锁仓</div>

          </template>
        </el-table-column>
        <el-table-column prop="orderFlag" label="委托" min-width="80">
          <template slot-scope="scope">
            <div v-if="scope.row.orderFlag == 1">可委托</div>
            <div v-if="scope.row.orderFlag == 2">不可委托</div>
          </template>
        </el-table-column>

        <!-- <el-table-column prop="marginRatio" label="保证金比例" min-width="80">
          <template slot-scope="scope">
            <span>{{scope.row.marginRatio}}</span>
          </template>
        </el-table-column> -->
        <el-table-column prop="fixedServiceCharge" label="固定服务费" min-width="80" />
        <!-- <el-table-column prop="serviceChargeRatio" label="服务费比例" min-width="80">
          <template slot-scope="scope">
            <span>{{scope.row.serviceChargeRatio}}</span>
          </template>
        </el-table-column> -->
        <el-table-column prop="dictLabel" label="行情源" min-width="80" />
        <el-table-column prop="lastTradingTime" label="最后交易时间" min-width="150" />
        <el-table-column prop="status" label="状态" min-width="80">
          <template slot-scope="scope">
            <el-button size="mini" v-if="scope.row.status == 1">正常</el-button>
            <el-button size="mini" v-if="scope.row.status == 2">关闭</el-button>
            <el-button size="mini" v-if="scope.row.status == 3">锁定</el-button>
            <el-button size="mini" v-if="scope.row.status == 4">所有</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" min-width="100" />
        <el-table-column prop="createTime" label="创建时间" min-width="150" />
        <el-table-column label="操作" min-width="130" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[15, 30, 45, 60]" :page-size="from.limit" :current-page="from.page"
          layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
          @current-change="pageChange" />
      </div>
    </el-card>

    <el-dialog :title="ruleForm.id ? '编辑' : '新增'" :visible.sync="dialogVisible" width="600px" top="5vh"
      :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :rules="rules" ref="ruleForm" :model="ruleForm" label-width="150px">
        <el-form-item label="品种名称：" prop="commodityId">
          <el-select v-model="ruleForm.commodityId" placeholder="请选择品种名称">
            <el-option v-for="item in options" :key="item.id" :label="item.commodityName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合约名称：" prop="contractName">
          <el-input v-model="ruleForm.contractName" placeholder="请输入合约名称" />
        </el-form-item>
        <el-form-item label="合约英文名称：" prop="contractNameEn" min-width="80">
          <el-input v-model="ruleForm.contractNameEn" placeholder="请输入合约英文名称" />
        </el-form-item>


        <!-- 锁仓、委托 -->
        <!-- <el-form-item label="锁仓：" prop="lockFlag">
          <el-select v-model="ruleForm.lockFlag" placeholder="请选择锁仓" clearable class="selWidth">
            <el-option v-for="item in lockData" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="委托：" prop="orderFlag">
          <el-select v-model="ruleForm.orderFlag" placeholder="请选择委托" clearable class="selWidth">
            <el-option v-for="item in orderData" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item> -->

        <el-table-column prop="fixedMargin" label="固定保证金" min-width="80" />
        <el-form-item label="合约代码：" prop="contractNo">
          <el-input v-model="ruleForm.contractNo" placeholder="请输入合约代码" />
        </el-form-item>
        <el-form-item label="合约完整代码：" prop="contractNoFull">
          <el-input v-model="ruleForm.contractNoFull" placeholder="请输入合约完整代码" />
        </el-form-item>
        <el-form-item label="固定保证金：" prop="fixedMargin">
          <el-input v-model="ruleForm.fixedMargin" placeholder="请输入固定保证金" />
        </el-form-item>
        <!-- <el-form-item label="保证金比例：" prop="marginRatio">
          <el-input v-model="ruleForm.marginRatio" placeholder="请输入保证金比例">
          </el-input>
        </el-form-item> -->
        <el-form-item label="固定服务费：" prop="fixedServiceCharge">
          <el-input v-model="ruleForm.fixedServiceCharge" placeholder="请输入固定服务费" />
        </el-form-item>
        <!-- <el-form-item label="服务费比例：" prop="serviceChargeRatio">
          <el-input v-model="ruleForm.serviceChargeRatio" placeholder="请输入服务费比例">
          </el-input>
        </el-form-item> -->
        <el-form-item label="行情源：" prop="marketSource">
          <el-select v-model="ruleForm.marketSource" placeholder="请选择行情源">
            <el-option v-for="item in marketSourceOptions" :key="item.dictValue" :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最后交易时间：">
          <el-date-picker v-model="ruleForm.lastTradingTime" :value-format="'yyyy-MM-dd HH:mm:ss'" type="datetime"
            placeholder="选择最后交易时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态：">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">关闭</el-radio>
            <el-radio :label="3">锁定</el-radio>
            <el-radio :label="4">所有</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input type="textarea" v-model="ruleForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close()">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  exchangecommodityGetOption,
  market_source,
  exchangecontractList,
  exchangecontractSave,
  exchangecontractInfo,
  exchangecontractUpdate,
  exchangecontractDelete,
  exchangecontractExport,

  getDictData
} from "@/api/transaction";
export default {
  name: "contract",
  data() {
    return {
      labelPosition: "right",
      options: [],
      marketSourceOptions: [],
      from: {
        commodityId: "",
        contractNo: "",
        contractNoFull: "",
        status: 1,
        page: 1,
        limit: 15,
      },
      loading: false,
      tableData: {
        data: [],
        total: 0,
      },
      selection: [],
      dialogVisible: false,
      ruleForm: {
        id: "",
        commodityId: "",
        contractNo: "",
        contractNoFull: "",
        contractName: "",
        fixedMargin: "",
        marginRatio: "",
        fixedServiceCharge: "",
        serviceChargeRatio: "",
        marketSource: "",
        lastTradingTime: "",
        status: 1,
        remark: "",

        lockFlag: '',
        orderFlag: ''
      },
      rules: {
        commodityId: [
          {
            required: true,
            message: "请选择品种名称",
            trigger: "change",
          },
        ],
        contractNo: [
          {
            required: true,
            message: "请输入合约代码",
            trigger: "blur",
          },
        ],
        contractNoFull: [
          {
            required: true,
            message: "请输入合约完整代码",
            trigger: "blur",
          },
        ],
        contractName: [
          {
            required: true,
            message: "请输入合约名称",
            trigger: "blur",
          },
        ],
        contractNameEn: [
          {
            required: true,
            message: "请输入合约英文名称",
            trigger: "blur",
          },
        ],
        fixedMargin: [
          {
            required: true,
            message: "请输入固定保证金",
            trigger: "blur",
          },
        ],
        marginRatio: [
          {
            required: true,
            message: "请输入保证金比例",
            trigger: "blur",
          },
        ],
        fixedServiceCharge: [
          {
            required: true,
            message: "请输入固定服务费",
            trigger: "blur",
          },
        ],
        serviceChargeRatio: [
          {
            required: true,
            message: "请输入服务费比例",
            trigger: "blur",
          },
        ],
        lockFlag: [{
          required: true,
          message: '请选择锁仓',
          trigger: 'change'
        }],
        orderFlag: [{
          required: true,
          message: '请选择委托',
          trigger: 'change'
        }],
      },

      // 字典数据
      lockData: [],
      orderData: [],
    };
  },
  mounted() {
    exchangecommodityGetOption()
      .then((res) => {
        console.log("品种全列表", res);
        this.options = res;
        this.getlist();
      })
      .catch((err) => {
        console.log(err);
      });
    market_source()
      .then((res) => {
        console.log("行情源列表", res);
        this.marketSourceOptions = res;
      })
      .catch((err) => {
        console.log(err);
      });


    // 字典数据
    // 锁仓
    // getDictData('lock_flag').then(res => {
    //   console.log('lock_flag 锁仓数据', res)
    //   this.lockData = res
    // }).catch(res => {
    //   console.log(res)
    // })
    // // 委托
    // getDictData('order_flag').then(res => {
    //   console.log('order_flag 委托数据', res)
    //   this.orderData = res
    // }).catch(res => {
    //   console.log(res)
    // })
  },
  methods: {
    search() {
      console.log(this.from);
      this.from.page = 1;
      this.from.limit = 15;
      this.getlist();
    },
    reset() {
      this.from = {
        commodityId: "",
        contractNo: "",
        contractNoFull: "",
        contractNameEn: "",
        status: 1,
        page: 1,
        limit: 15,
      };
      this.getlist();
    },
    getlist() {
      this.loading = true;
      exchangecontractList(this.from)
        .then((res) => {
          console.log(res);
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          this.loading = false;
        })
        .catch((err) => {
          console.log(err);
          this.loading = false;
        });
    },
    add() {
      this.ruleForm = {
        id: "",
        commodityId: "",
        contractNo: "",
        contractNameEn: "",
        contractNoFull: "",
        contractName: "",
        fixedMargin: "",
        marginRatio: "",
        fixedServiceCharge: "",
        serviceChargeRatio: "",
        marketSource: "",
        lastTradingTime: "",
        status: 1,
        remark: "",

      };
      this.dialogVisible = true;
    },
    close() {
      this.ruleForm = {
        id: "",
        commodityId: "",
        contractNo: "",
        contractNameEn: "",
        contractNoFull: "",
        contractName: "",
        fixedMargin: "",
        marginRatio: "",
        fixedServiceCharge: "",
        serviceChargeRatio: "",
        marketSource: "",
        lastTradingTime: "",
        status: 1,
        remark: "",

      };
      this.dialogVisible = false;
    },
    submit() {
      if (!this.ruleForm.lastTradingTime) {
        return this.$message.error("请选择最后交易时间");
      }
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            console.log("编辑", this.ruleForm);
            exchangecontractUpdate(this.ruleForm)
              .then((res) => {
                console.log(res);
                this.close();
                this.$message.success("编辑成功");
                this.getlist();
              })
              .catch((err) => {
                console.log(err);
                this.$message.error(err);
              });
          } else {
            console.log("新增", this.ruleForm);
            let data = {
              commodityId: this.ruleForm.commodityId,
              contractNo: this.ruleForm.contractNo,
              contractNameEn: this.ruleForm.contractNameEn,
              contractNoFull: this.ruleForm.contractNoFull,
              contractName: this.ruleForm.contractName,
              fixedMargin: this.ruleForm.fixedMargin,
              marginRatio: this.ruleForm.marginRatio,
              fixedServiceCharge: this.ruleForm.fixedServiceCharge,
              serviceChargeRatio: this.ruleForm.serviceChargeRatio,
              marketSource: this.ruleForm.marketSource,
              lastTradingTime: this.ruleForm.lastTradingTime,
              status: this.ruleForm.status,
              remark: this.ruleForm.remark,


            };
            console.log(data)
            exchangecontractSave(data)
              .then((res) => {
                console.log(res);
                this.close();
                this.$message.success("新增成功");
                this.getlist();
              })
              .catch((err) => {
                console.log(err);
                this.$message.error(err);
              });
          }
        } else {
          this.$message.error("请填写完整信息");
        }
      });
    },
    // 选择
    onSelectTab(selection) {
      this.selection = [];
      selection.forEach((item) => {
        this.selection.push(item.id);
      });
      console.log(this.selection);
    },
    allDel() {
      if (this.selection.length == 0) {
        return this.$message.error("至少选择一项");
      }
      console.log(this.selection);
      this.$confirm("确认删除？")
        .then((_) => {
          exchangecontractDelete(this.selection)
            .then((res) => {
              console.log(res);
              this.$message.success("删除成功");
              this.getlist();
            })
            .catch((err) => {
              console.log(err);
              this.$message.error(err);
            });
        })
        .catch((_) => { });
    },
    exportTable() {
      exchangecontractExport(this.from)
        .then((res) => {
          console.log("导出表格", res);
          window.open(res.filename);
        })
        .catch((err) => {
          console.log(err);
          this.$message.error(err);
        });
    },
    handleEdit(row) {
      console.log("编辑", row);
      exchangecontractInfo(row.id)
        .then((res) => {
          console.log("详情", res);
          this.ruleForm = {
            id: res.id,
            commodityId: res.commodityId,
            contractNo: res.contractNo,
            contractNameEn: res.contractNameEn,
            contractNoFull: res.contractNoFull,
            contractName: res.contractName,
            fixedMargin: res.fixedMargin,
            marginRatio: res.marginRatio,
            fixedServiceCharge: res.fixedServiceCharge,
            serviceChargeRatio: res.serviceChargeRatio,
            marketSource: String(res.marketSource),
            lastTradingTime: res.lastTradingTime,
            status: Number(res.status),
            remark: res.remark,

          };
          this.dialogVisible = true;
        })
        .catch((err) => {
          console.log(err);
          this.$message.error(err);
        });
    },
    handleDel(row) {
      this.$confirm("确认删除？")
        .then((_) => {
          console.log("删除", row);
          exchangecontractDelete([row.id])
            .then((res) => {
              console.log(res);
              this.$message.success("删除成功");
              this.getlist();
            })
            .catch((err) => {
              console.log(err);
              this.$message.error(err);
            });
        })
        .catch((_) => { });
    },
    pageChange(page) {
      this.from.page = page;
      this.getlist();
    },
    handleSizeChange(val) {
      this.from.limit = val;
      this.getlist();
    },
  },
};
</script>

<style scoped lang="scss">
.btn_bt {
  border-top: 1px dashed #ccc;
  padding-top: 20px;
}
</style>
